﻿using Sl.Duplex;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sl.Forms {
	public partial class SerialPortAccess : Form {
		public SerialPortAccess() {
			InitializeComponent();
			//这段代码曾经被某种重新生成吃了，在自动生成文件里。
			this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
			this.ConnectStatusBTN,
			this.PortSelectCB,
			this.SerialSetTB});
			//------------------------------
			mainStm.Port = new SerialPortDp();
			mainStm.Port.ePortStatusChange += Port_ePortStatusChange;
			searchPort();
			if (PortSelectCB.Items.Count != 0) {
				PortSelectCB.SelectedIndex = 0;
			}
			SerialSetTB.ToolTipText = Sl.Duplex.SerialPort_expand.Help;
		}
		public void searchPort() {
			string[] avariablePort_names = System.IO.Ports.SerialPort.GetPortNames();
			PortSelectCB.Items.Clear();
			if (avariablePort_names.Length != 0) {
				PortSelectCB.Items.AddRange(avariablePort_names);
			}
		}
		private void portSettingChanged(object sender, EventArgs e) {
			if (mainStm.Port != null) {
				mainStm.Port.closePort();
			}
			SerialPort sp = (mainStm.Port as SerialPortDp).sp;
			string warning = sp.init($"{PortSelectCB.Text},{SerialSetTB.Text}");
			if (warning == "") {
				mainStm.Port.openPort();
				SerialSetTB.ForeColor = Color.Black;
				SerialSetTB.ToolTipText = Sl.Duplex.SerialPort_expand.Help;
			} else {
				SerialSetTB.ForeColor = Color.FromArgb(153, 77, 0);
				SerialSetTB.ToolTipText = warning + "\n-------------------\n\n" + Sl.Duplex.SerialPort_expand.Help;
			}
			Port_ePortStatusChange(mainStm.Port);
		}


		Image connected = Sl.Support.Forms.IconRecolor.reColorPng(global::CmdTextBox.Properties.Resources.connected_30px, Color.FromArgb(0, 77, 153));
		Image disconnected = Sl.Support.Forms.IconRecolor.reColorPng(global::CmdTextBox.Properties.Resources.disconnected_30px, Color.FromArgb(153, 77, 0));

		private void Port_ePortStatusChange(IDuplex obj) {
			if (mainStm.Port.IsRunning) {
				ConnectStatusBTN.Image = connected;
				ConnectStatusBTN.Text = "断开串口";
			} else {
				ConnectStatusBTN.Image = disconnected;
				ConnectStatusBTN.Text = "连接串口";
			}
		}

		private void ConnectStatusBTN_Click(object sender, EventArgs e) {
			if (mainStm.Port.IsRunning) {
				mainStm.Port.closePort();
			} else {
				mainStm.Port.openPort();

			}
		}
		private void SpeedUpdateTimer_Tick(object sender, EventArgs e) {
			mainStm.CheckSpeedLoop();
			SpeedLAB.Text = $"收/发字节数={mainStm.Send_count}B/{mainStm.Recv_count}B";
			//	$"收/发速率={mainStm.Send_speed:f2}Bps/{mainStm.Recv_speed:f2}Bps";
		}


		//事件代码
		private const int WM_DEVICECHANGE = 0x219; //设备改变
		private const int DBT_DEVICEARRIVAL = 0x8000; //检测到新设备
		private const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //移除设备

		protected override void WndProc(ref Message m) {
			base.WndProc(ref m);//调用父类方法，以确保其他功能正常
			if (m.Msg == WM_DEVICECHANGE) {
				Console.WriteLine($"dev change{m.Msg},{(int)m.WParam}");
				if (((int)m.WParam == DBT_DEVICEARRIVAL) || ((int)m.WParam == DBT_DEVICEREMOVECOMPLETE)) {
					searchPort();
					Console.WriteLine("USB change");
				}
			}
		}

		private void Port_ePortStatusChange(object sender, EventArgs e) {

		}
	}
}
